/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Controller.CustomerManager;
import Controller.LogManager;
import java.io.IOException;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author pauld
 */
@WebServlet(name="ForgotPW", urlPatterns={"/ForgotPW"})
public class ForgotPW extends HttpServlet {
    private static String username = null,
            email = null;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        
        int result = CustomerManager.forgotPW(username, email);
        
        switch( result ){
            //if the user is locked out
            case 2: 
                LogManager.logEvent("ForgotPW." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.WARNING, "Forgot Password Unsuccessful - User Locked Out: " + username, request.getRemoteAddr(), "Not Logged In");
                response.sendRedirect("forgotPW.jsp?result=2");
                break;
            //if the password is reset successfuly 
            case 1: 
                LogManager.logEvent("ForgotPW." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.INFO, "Forgot Password Successful: " + username, request.getRemoteAddr(), "Not Logged In");
                response.sendRedirect("forgotPW.jsp?result=1");
                break;
            //if the username or passoword is not correct
            case 0: 
                LogManager.logEvent("ForgotPW." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.WARNING, "Forgot Password Unsuccessful - Wrong Credentials", request.getRemoteAddr(), "Not Logged In");
                response.sendRedirect("forgotPW.jsp?result=0");
                break;
            //if the database has a problem
            case -1: 
                LogManager.logEvent("ForgotPW." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.SEVERE, "Forgot Password Unsuccessful - Database Error", request.getRemoteAddr(), "Not Logged In");
                response.sendRedirect("forgotPW.jsp?result=-1");
                break;
            
        }
        
    }
    
    private static boolean checkInput(HttpServletRequest request){
        username = request.getParameter("username");
        email = request.getParameter("email");
        
        String regexEmail = "^[A-Za-z0-9._%'-]+@[A-Za-z0-9.-]+\\.[a-zA-Z]{2,4}$",
                regexAlphaNum = "^[A-Za-z0-9]+$";
        Pattern pEmail = Pattern.compile(regexEmail, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE),
                pAlphaNum = Pattern.compile(regexAlphaNum, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
        Matcher mEmail = pEmail.matcher(email),
                mUsername = pAlphaNum.matcher(username);
        boolean resultEmail = mEmail.find(),
                resultUsername = mUsername.find();
        
        if( (username.length() > 0 && username.length() <= 25) &&
            (email.length() > 0 && email.length() <= 25) &&
                resultEmail &&
                resultUsername){
            return true;
        }
        else{            
            LogManager.logEvent("ForgotPW." + Thread.currentThread().getStackTrace()[1].getMethodName(), Level.WARNING, "Forgot Password Unsuccessful - Invalid Input", request.getRemoteAddr(), "Not Logged In");
            return false;
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //check the input of the user before processing
        if( checkInput(request) )
            processRequest(request, response);
        else
            response.sendRedirect("register.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //check the input of the user before processing
        if( checkInput(request) )
            processRequest(request, response);
        else
            response.sendRedirect("register.jsp");
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
    
}
